import React, {Component} from 'react';
import {Space, Table, Button, Form, Input} from 'antd';
import {AllSchools, AllSubjects} from "@/services/DashApi";
import {nanoid, PageContainer} from "@ant-design/pro-components";
import {history, Link} from "umi";
import {ProvinceMenu} from "@/services/Province/ProvinceApi";

const SearchModule = (props) => {

  const {onFilterName} = props

  return (
    <Form
      name="basic"
      layout="inline"
      labelCol={{span: 8}}
      wrapperCol={{span: 16}}
      initialValues={{remember: true}}
      onFinish={(values) => {
        onFilterName(values.keyword)
      }}
      autoComplete="off"
    >
      <Space>
        <Form.Item
          name="keyword"
        >
          <Input style={{width: 163}} name="searchValue" placeholder="请输入关键字"/>
        </Form.Item>
        <Button
          type="primary"
          htmlType="submit"
        >
          搜索
        </Button>
        <Button
          type="default"
          onClick={() => {
            onFilterName("")
          }}
        >
          全部
        </Button>
      </Space>
    </Form>
  )
}

class SchoolLists extends Component {

  state = {
    columns: [
      {
        title: '学科',
        dataIndex: 'name',
        key: 'name',
        render: text => <Link to={"/province/item_dashboard?name="+text}><Button type="link">{text}</Button></Link>,
      },
      // {
      //   title: '层次',
      //   dataIndex: 'progress',
      //   key: 'progress',
      //   sorter: (a, b) => a.progress - b.progress,
      //   render: text => <span>{text}</span>,
      // },
    ],
    data: [],
    originDataSource: [],
    loading: true
  }

  handleFilter = (keyword) => {
    if (!keyword) keyword = ""
    if (this.state.originDataSource.length === 0) {
      this.state.originDataSource = this.state.data
    }
    console.log(this.state.originDataSource.length)
    this.state.data = this.state.originDataSource.filter((current) => {
      return current.name.indexOf(keyword) !== -1})
    this.setState({data: this.state.data})
  }

  componentDidMount() {
    // AllSubjects().then(response => {
    //   console.log("@查询所有学科", response)
    //   const res = []
    //   for (let i = 0; i < response.discipline.length; i++) {
    //     res[i] = {
    //       key: `${i + 1}`,
    //       name: response.discipline[i].discipline_name,
    //       number: response.discipline[i].discipline_code,
    //       progress: response.discipline[i].level,
    //     }
    //   }
    //   this.setState({data: res, loading: false})
    //   console.log("@res", res)
    // }, () => {
    //   this.setState({loading: false})
    // })

    // const name = history.location.query.name
    //
    // if (name === "新材料创新发展") {
    //   const dataSource = [
    //     {
    //       key: 1,
    //       name: '材料科学与工程',
    //       number: '南昌大学',
    //       progress: '南昌大学',
    //     }
    //   ]
    //   this.setState({loading: false, data:dataSource})
    // } else if (name === "绿色食品") {
    //   const dataSource = [
    //     {
    //       key: 1,
    //       name: '食品科学与工程',
    //       number: '南昌大学',
    //       progress: '南昌大学',
    //     }
    //   ]
    //   this.setState({loading: false, data:dataSource})
    // } else if (name === "临床医学与公共卫生大健康") {
    //   const dataSource = [
    //     {
    //       key: 1,
    //       name: '临床医学',
    //       number: '南昌大学',
    //       progress: '南昌大学',
    //     },
    //     {
    //       key: 1,
    //       name: '口腔医学',
    //       number: '南昌大学',
    //       progress: '南昌大学',
    //     }
    //   ]
    //   this.setState({loading: false, data:dataSource})
    // } else if (name === "红色基因传承") {
    //   const dataSource = [
    //     {
    //       key: 1,
    //       name: '临床医学',
    //       number: '南昌大学',
    //       progress: '南昌大学',
    //     }
    //   ]
    //   this.setState({loading: false, data:dataSource})
    // } else if (name === "流域碳中和") {
    //   const dataSource = [
    //     {
    //       key: 1,
    //       name: '临床医学',
    //       number: '南昌大学',
    //       progress: '南昌大学',
    //     }
    //   ]
    //   this.setState({loading: false, data:dataSource})
    // } else if (name === "人工智能与先进制造") {
    //   const dataSource = [
    //     {
    //       key: 1,
    //       name: '临床医学',
    //       number: '南昌大学',
    //       progress: '南昌大学',
    //     }
    //   ]
    //   this.setState({loading: false, data:dataSource})
    // }
    // this.setState({loading: false})

    ProvinceMenu().then(response => {
      console.log('@返回数据', response)
      const name = history.location.query.name
      const data = response.data
      let dataSource = []
      for (let i = 0; i < data.length; i++) {
        if (data[i].univ_name === name) {
          dataSource.push({
            key: nanoid(),
            name: data[i].discipline_name,
            number: '0001',
            progress: '0001'
          })
        }
      }
      this.setState({loading: false, data:dataSource})
    })



  }

  render() {
    return (
      <PageContainer header={{
        breadcrumb: {},
      }}
      >
        <Space direction="vertical" size="middle" style={{display: 'flex'}}>
          <SearchModule onFilterName={this.handleFilter} />
          <Table<any>
            loading={this.state.loading}
            rowKey={(record) => record.index}
            size="small"
            columns={this.state.columns}
            dataSource={this.state.data}
            pagination={{
              // style: { marginBottom: 0 },
              pageSize: 20,
              showSizeChanger: false,
              showQuickJumper: false
            }}
          />
        </Space>
      </PageContainer>
    );
  }
}

export default SchoolLists;

